package com.cmcc.aregister.ui.activity;

import java.util.Map;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class YichengtongActivity extends LoginParentActivity
{
    private Button button;
    private String params;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init(R.layout.yichengtong_layout);
        mHttpManager.setCxt(getParent());
        setContentView(R.layout.yichengtong_layout);
        button = (Button) findViewById(R.id.open_yichengtong);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (!ConfigUtilHelper.isLogin(YichengtongActivity.this))
                {
                    Intent intent = new Intent(YichengtongActivity.this,
                        UserLoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    return;
                }
                kaitongDlg(getString(R.string.yewuxieyi_title),
                    getString(R.string.yewuxieyi_conent),
                    getString(R.string.querenkaitong),
                    getString(R.string.quxiao));
            }
        });
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        // 检查医程通用户
        checkYct();
    }

    private HttpAsyncTask checkYctTask;

    private void checkYct()
    {
        params = "&client_auth=test&token=123456&mobile="
            + ConfigUtilHelper.getMobile(app);
        checkYctTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_CHECK_YCT, Arguments.CHECK_YCT_URL, params,
            HttpAsyncTask.CACHE_TYPE_ACTION, true);
        showProgressDialogNew(R.string.loading);
        checkYctTask.setUIHandler(checkYctTaskHandler);
        mHttpManager.execute(checkYctTask);
    }

    private Handler checkYctTaskHandler = new Handler() {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null)
                {
                    Map<String, Object> map = (Map<String, Object>) msg.obj;
                    String status = (String) map.get(Arguments.XML_STATUS);
                    if (status.equals("1"))
                    {
                        button.setVisibility(View.VISIBLE);
                    }
                }
                else
                {
                    showMsg(R.string.read_member_fail);
                }
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.query_yichengtong),
                    getString(R.string.close));
                break;
            }
        };
    };

    private void kaitongDlg(String title, String msg, String posTitle,
        String nagativeTitle)
    {
        new AlertDialog.Builder(this)
            .setTitle(title)
            .setMessage(msg)
            .setIcon(android.R.drawable.ic_dialog_info)
            .setCancelable(false)
            .setPositiveButton(posTitle, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    sendSMS("B");
                }
            })
            .setNegativeButton(nagativeTitle,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                    }

                }).create().show();
    }

    private void sendSMS(String smsBody)

    {
        Uri smsToUri = Uri.parse("smsto:1065703097");
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        startActivity(intent);
    }
}
